<?php
/*
 * Created on 9 juil. 08
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 
 
 
 class FormData{
 	private $formElems = Array(); 	
 	private $formEvents = Array(); 	
 	
 	function getElemValue($name){
 		return $this->formElems[$name]; 		
 	}
 	
 	function addFormElement($formElement){
 		$size = count($this -> formElems);
 		$this->formElems[$size] = $formElement;
 	}
 	
 	function addFormEvent($elementId, $eventName){
 		$size = count($this -> formEvents);
 		$this->formEvents[$size] = new FormEvent($elementId, $eventName);
 	}
 	
 	function buildView(){
 		echo '<script src="/MyPHP/core/js/viewbinding.js" type="text/javascript"></script>';
 		echo "\n";
 		echo '<script  type="text/javascript">';
 		echo "\n";
 		echo ' var viewControl = new ViewControl();';
 		echo "\n";
 		foreach ($this->formElems as $formElem){
 			$formElem -> buildJsCode();
 		}
 		
 		foreach ($this->formEvents as $formEvent){
 			$formEvent -> buildJsCode(); 			
 		} 		
 		echo "var onLoadEvent = viewControl.windowOnLoadEvent();\n";
 		echo "if(window.addEventListener) {\n";
		echo "	window.addEventListener('load', onLoadWindow, true);\n";	
		echo "	window.dispatchEvent(onLoadEvent);\n";					
		echo "}\n";
		echo "else if(window.attachEvent) {\n";
		echo "	element.attachEvent('onLoad', onLoadWindow);\n";
		echo "	window.fireEvent('onLoad',onLoadEvent);\n";				
		echo "}\n";
 		echo "\n";
 		echo '</script>';
 		echo "\n";
 		
 	}
 	
 }
 
 class FormEvent{
 	private $elementId;
 	private $eventName;
 	function __construct($_elementId, $_eventName){
 		$this -> elementId = $_elementId;
 		$this -> eventName = $_eventName; 		
 	}
 	
 	public function buildJsCode(){
 		echo "viewControl.addEventElement('".$this -> elementId."','".$this -> eventName."');\n";
 	}
 }
 
 class FormElement{
 	private $elemId;
 	private $elemValue;	
 	private $visible = true; 	
 	
 	function __construct($_elemId, $_elemValue){
 		$this -> elemId = $_elemId;
 		$this -> elemValue = $_elemValue;
 		$this -> visible = true;
 	}
 	
 	public function buildJsCode(){
 		echo 'viewControl.addElement("'.$this -> getElemId().'", "'.$this -> getElemValue().'");';
 		echo "\n";
 	}
 	
 	public function setVisible($_visible){
 		$this -> visible = $_visible;
 	}
 	
 	public function getElemId(){
 		return $this -> elemId;
 	}
 	
 	public function getElemValue(){
 		return $this -> elemValue;
 	} 	
 }
 
 class TextElement extends FormElement{
 	function __construct($_elemId, $_elemValue){
 		parent::__construct($_elemId, $_elemValue);
 	}
 	
 	public function buildJsCode(){
 		echo 'viewControl.addTextElement("'.$this -> getElemId().'", "'.$this -> getElemValue().'");';
 		echo "\n";
 	}
 	
 }
 
//	echo '<pre>';
//	 $form = new FormData();
//	 $form->addFormElement(new FormElement("USER.FIRST_NAME", "First name")); 	
// 	 $form->addFormElement(new FormElement("USER.LAST_NAME", "Last name"));
// 	 $form->addFormElement(new TextElement("USER.FULL_NAME", "Full name"));
//	 $form->buildView();
//	echo '</pre>';
 
?>
